package cn.wolfcode.ssm.service.impl;

import cn.wolfcode.ssm.domain.Employee;
import cn.wolfcode.ssm.mapper.EmployeeMapper;
import cn.wolfcode.ssm.page.PageResult;
import cn.wolfcode.ssm.query.QueryObject;
import cn.wolfcode.ssm.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class IEmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;



    public void save(Employee employee, Long[] ids) {
        // 在员工表插入数据之后在员工和角色表 插入数据
        employeeMapper.insert(employee);

        // 关联中间表
        // 一定要在员工表执行插入语句之后,在执行这条Sql语句  不然获取的employee.getId()就是null;
        // 先判断ids是否为空.
        if (ids != null && ids.length > 0) {
            // 遍历获取的ids中的元素,取出每一个值
            for (Long rid : ids) {
                // 执行Sql语句,记住不用在IEmployeeService 写抽象方法.
                // 要在EmployeeMapper 和EmployeeMapper.xml中写对应的方法和Sql语句
                employeeMapper.insertReslation(employee.getId(),rid);
            }
        }


    }

    @Override
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);

        // 更新之前要把中间表的数据先删除,不然会造成数据重复
        employeeMapper.deleteReslation(employee.getId());

        // 关联中间表
        // 一定要在员工表执行插入语句之后,在执行这条Sql语句  不然获取的employee.getId()就是null;
        // 先判断ids是否为空.
        if (ids != null && ids.length > 0) {
            // 遍历获取的ids中的元素,取出每一个值
            for (Long rid : ids) {
                // 执行Sql语句,记住不用在IEmployeeService 写抽象方法.
                // 要在EmployeeMapper 和EmployeeMapper.xml中写对应的方法和Sql语句
                employeeMapper.insertReslation(employee.getId(),rid);
            }
        }

    }

    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
        employeeMapper.deleteReslation(id);

    }

    @Override
    public Employee get(Long id) {

        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageResult<Employee> query(QueryObject qo) {
        int totalcount = employeeMapper.totalcount(qo);
        if (totalcount == 0) {
            return new PageResult<Employee>(qo.getCurrentPage(), qo.getPageSize());
        }else{
            List<Employee> PageResult = employeeMapper.result(qo);
            return new PageResult<Employee>(qo.getCurrentPage(),qo.getPageSize(),totalcount,PageResult);
        }
    }

    @Override
    public Employee login(String username, String password) {
        //查询数据库中是否有匹配的数据
        Employee employee =  employeeMapper.selectedByUsernameAndPassword(username,password);
        if(employee == null){
            throw new RuntimeException("账号或密码错误");
        }
        return  employee;
    }

    @Override
    public String contrastPassword(Long id) {
        String password = employeeMapper.updatePwd(id);
        return password;
    }

    @Override
    public void updatePassword(Long id,String newPassword) {
        employeeMapper.updatePassword(id,newPassword);
    }

    @Override
    public void resetPwd(String newPassword, Long userId) {
        employeeMapper.resetPwd(newPassword,userId);
    }


}
